package com.dzl.micro.service.impl;

import com.dzl.base.pojo.BaseData;
import com.dzl.base.service.BaseDataService;
import com.dzl.base.service.BaseServiceImpl;
import com.dzl.brand.pojo.WelfareBrand;
import com.dzl.config.exception.CommonException;
import com.dzl.constant.CommonConstants;
import com.dzl.goods.pojo.WelfareGoodsClassify;
import com.dzl.goods.pojo.query.WelfareGoodsPageQuery;
import com.dzl.goods.pojo.vo.WelfareGoodsBaseVo;
import com.dzl.goods.pojo.vo.WelfareGoodsClassifyVo;
import com.dzl.goods.service.WelfareGoodsClassifyService;
import com.dzl.goods.service.WelfareGoodsService;
import com.dzl.micro.dao.WelfareMicroPageMapper;
import com.dzl.micro.pojo.*;
import com.dzl.micro.pojo.vo.WelfareMicroManageGoodsVo;
import com.dzl.micro.pojo.vo.WelfareMicroPageWithSwitchVo;
import com.dzl.micro.pojo.vo.WelfareMicroVo;
import com.dzl.micro.query.WelfareMicroPageQuery;
import com.dzl.micro.service.*;
import com.dzl.moive.pojo.WelfareMovieInfo;
import com.dzl.partition.pojo.WelfarePartition;
import com.dzl.partition.service.WelfarePartitionService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.ImageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 微页面表(WelfareMicroPage)表服务实现类
 * @author ChenLong
 * @date 2021-07-30 11:10:53
 */
@Slf4j
@Service
public class WelfareMicroPageServiceImpl extends BaseServiceImpl<WelfareMicroPage> implements WelfareMicroPageService {

    @Autowired
    private WelfareMicroPageMapper mapper;
    @Autowired
    private WelfareGoodsService welfareGoodsService;
    @Autowired
    private WelfareGoodsClassifyService welfareGoodsClassifyService;
    @Autowired
    private WelfareMicroManageService welfareMicroManageService;
    @Autowired
    private WelfareMicroGoodsService welfareMicroGoodsService;
    @Autowired
    private WelfareMicroSwitchService welfareMicroSwitchService;
    @Autowired
    private WelfarePartitionService welfarePartitionService;
    @Autowired
    private WelfareMicroPartitionService welfareMicroPartitionService;
    @Autowired
    private BaseDataService baseDataService;

   /* @Override
    public List<WelfareMicroManageGoodsVo> getMicroPageTabByPageId(Integer pageId) {
        return null;
    }*/

    @Override
    public WelfareMicroVo getMicroPageByPageId(Integer pageId, Integer port) {
        WelfareMicroVo welfareMicroVo = new WelfareMicroVo();
        List<WelfareMicroManageGoodsVo> microManageGoodsVoList = mapper.getWelfareMicroPageList(pageId);
        /*if (pageId.equals(0)) {
            microManageGoodsVoList = mapper.getWelfareMicroMainPageList(pageId);
        } else {
            microManageGoodsVoList = mapper.getWelfareMicroPageList(pageId);
        }*/
        final Integer finalPageId = pageId;
        if (EmptyUtils.isNotEmpty(microManageGoodsVoList) && microManageGoodsVoList.size() > 0) {
            microManageGoodsVoList.forEach(microManageGoods -> {
                if(port == CommonConstants.DATA_PORT.SERVER){
                    log.info("================================================服务端页面加载");
                    if (EmptyUtils.isNotEmpty(microManageGoods.getLinkType())){
                        if ( microManageGoods.getLinkType().equals(4)) {
                            WelfareGoodsPageQuery welfareGoodsPageQuery = new WelfareGoodsPageQuery();
                            welfareGoodsPageQuery.setMicroId(microManageGoods.getId());
                            welfareGoodsPageQuery.setPageSize(99999);
                            WelfareMicroPage welfareMicroPageRes = mapper.selectByPrimaryKey(finalPageId);
                            if (EmptyUtils.isNotEmpty(welfareMicroPageRes) && EmptyUtils.isNotEmpty(welfareMicroPageRes.getIsMovie()) && welfareMicroPageRes.getIsMovie().equals(1)){
                                WelfareMovieInfo movieInfo = welfareGoodsService.getGoodsLinkByMicroIdForMovie(microManageGoods.getId());
                                if (EmptyUtils.isNotEmpty(movieInfo)){
                                    microManageGoods.setMovieInfo(movieInfo);
                                    if (EmptyUtils.isNotEmpty(movieInfo.getName())){
                                        microManageGoods.setUrlTitle(movieInfo.getName());
                                    }
                                }
                            } else {
                                List<WelfareGoodsBaseVo> goodsBaseVoList = welfareGoodsService.getGoodsListPageByMicroId(welfareGoodsPageQuery).getList();
                                if(microManageGoods.getType() == 4){ //如果是底部标签栏，则返回商品数据列表
                                    List<WelfareGoodsBaseVo> goodsBaseVoList_ = new ArrayList<>();
                                    for (int i=0;i<goodsBaseVoList.size();i++) {
                                        goodsBaseVoList_.add(goodsBaseVoList.get(i));
                                        if(i>=3){
                                            break;
                                        }
                                    }
                                    microManageGoods.setGoodsBaseVoList(goodsBaseVoList_);
                                }
                                //microManageGoods.setGoodsBaseVoList(goodsBaseVoList);
                                List<Integer> list = new ArrayList<>();
                                List<String> goodsSortlist = new ArrayList<>();
                                if (EmptyUtils.isNotEmpty(goodsBaseVoList)) {
                                    goodsBaseVoList.forEach(x -> {
                                        list.add(x.getId());
                                        if(EmptyUtils.isNotEmpty(x.getSort())){
                                            goodsSortlist.add(x.getId()+"_"+x.getSort());
                                        }
                                    });
                                }
                                microManageGoods.setGoodsIds(list);
                                microManageGoods.setGoodsSorts(goodsSortlist);
                                //int count = welfareMicroGoodsService.selectGoodsCountByMicroId(microManageGoods.getId());
                                microManageGoods.setUrlTitle(EmptyUtils.isEmpty(list) ? "请选择商品" : "已添加商品" + list.size() + "个");
                            }
                        }else if (microManageGoods.getLinkType() == 1) {
                            WelfarePartition welfarePartition = welfarePartitionService.selectByPrimaryKey(microManageGoods.getPartitionId());
                            if (EmptyUtils.isNotEmpty(welfarePartition)) {
                                microManageGoods.setUrlTitle("分区：" + welfarePartition.getPartitionName());
                            }
                        } else if (microManageGoods.getLinkType() == 2) {
                            String msg = "分类：";
                            if (EmptyUtils.isNotEmpty(microManageGoods.getClassifyId())) {
                                String[] split = microManageGoods.getClassifyId().split(",");
                                for (String s : split) {
                                    WelfareGoodsClassify welfareGoodsClassify = welfareGoodsClassifyService.selectByPrimaryKey(s);
                                    if (EmptyUtils.isNotEmpty(welfareGoodsClassify)) {
                                        msg = msg + welfareGoodsClassify.getName() + "/";
                                    }
                                }
                            }
                            microManageGoods.setUrlTitle(msg);
                        }else if (microManageGoods.getLinkType() == 3){
                            String msg = "分类：";
                            if (EmptyUtils.isNotEmpty(microManageGoods.getClassifyId())) {
                                String[] split = microManageGoods.getClassifyId().split(",");
                                for (String s : split) {
                                    WelfareGoodsClassify welfareGoodsClassify = welfareGoodsClassifyService.selectByPrimaryKey(s);
                                    if (EmptyUtils.isNotEmpty(welfareGoodsClassify)) {
                                        msg = msg + welfareGoodsClassify.getName() + "/";
                                    }
                                }
                            }
                            if (EmptyUtils.isNotEmpty(microManageGoods.getPartitionId())){
                                WelfarePartition welfarePartition = welfarePartitionService.selectByPrimaryKey(microManageGoods.getPartitionId());
                                if (EmptyUtils.isNotEmpty(welfarePartition)) {
                                    msg = msg + " 分区：" +welfarePartition.getPartitionName();
                                }
                            }
                            microManageGoods.setUrlTitle(msg);
                        }  else if (microManageGoods.getLinkType() == 5) {
                            microManageGoods.setUrlTitle("URL");
                        } else if (microManageGoods.getLinkType() == 6) {
                            WelfareMicroPage welfareMicroPage = mapper.selectByPrimaryKey(microManageGoods.getLinkPageId());
                            if (EmptyUtils.isNotEmpty(welfareMicroPage)) {
                                microManageGoods.setUrlTitle("微页面：" + welfareMicroPage.getName());
                            }
                        } else if (microManageGoods.getLinkType() == 7) {
                            microManageGoods.setUrlTitle("不跳转");
                        } else if (microManageGoods.getLinkType() == 8) {
                            WelfareMicroPage welfareMicroPage = mapper.selectByPrimaryKey(microManageGoods.getLinkPageId());
                            microManageGoods.setUrlTitle("微页面：" + welfareMicroPage.getName());
                        }
                    }else {
                        microManageGoods.setUrlTitle("-");
                    }
                }else{
                    log.info("================================================小程序端页面加载");
                    // 如果跳转类型为跳商品，则获取商品Id列表
                    if (EmptyUtils.isNotEmpty(microManageGoods.getLinkType()) && microManageGoods.getLinkType().equals(4)) {
                        List<WelfareGoodsBaseVo> goodsBaseVoList = new ArrayList<>();
                        Integer count = welfareMicroGoodsService.selectGoodsCountByMicroId(microManageGoods.getId());
                        if(count>1){//如果有多个商品，则构建2个虚拟商品，设置goodsBaseVolist的长度为2，便于前端跳转条件判断
                            WelfareGoodsBaseVo welfareGoodsBaseVo = new WelfareGoodsBaseVo();
                            welfareGoodsBaseVo.setId(0);
                            goodsBaseVoList.add(welfareGoodsBaseVo);
                            WelfareGoodsBaseVo welfareGoodsBaseVo1 = new WelfareGoodsBaseVo();
                            welfareGoodsBaseVo1.setId(1);
                            goodsBaseVoList.add(welfareGoodsBaseVo1);
                        }else if(count==1){//如果是一个商品，则查询此商品并赋值给goodsBaseVoList
                            WelfareGoodsPageQuery welfareGoodsPageQuery = new WelfareGoodsPageQuery();
                            welfareGoodsPageQuery.setMicroId(microManageGoods.getId());
                            goodsBaseVoList = welfareGoodsService.getGoodsListPageByMicroId(welfareGoodsPageQuery).getList();
                            //microManageGoods.setGoodsBaseVoList(goodsBaseVoList);
                        }
                        microManageGoods.setGoodsBaseVoList(goodsBaseVoList);
                    }
                }
                if (EmptyUtils.isNotEmpty(microManageGoods.getImg())) {
                    microManageGoods.setImg(ImageUtils.setImageSrc(microManageGoods.getImg()));
                }
            });
            // 过滤轮播类型列表（轮播图不受开关控制）
            List<WelfareMicroManageGoodsVo> shufflingList = microManageGoodsVoList.stream().filter(welfareMicroManage -> welfareMicroManage.getType() == 1).collect(Collectors.toList());
            if (EmptyUtils.isNotEmpty(shufflingList)) {
                // 根据sort升序排序
                shufflingList = shufflingList.stream().sorted(Comparator.comparing(WelfareMicroManage::getSort)).collect(Collectors.toList());
                welfareMicroVo.setShufflingList(shufflingList);
            }
        }
        if (pageId!=null && pageId.equals(0)) {
            pageId = microManageGoodsVoList.get(0).getPageId();
        }
        // 获取开关和微页面信息
        WelfareMicroPageWithSwitchVo pageInfoWithSwitchRes = mapper.getMicroPageInfoWithSwitch(pageId);
        welfareMicroVo.setSwitchTab(pageInfoWithSwitchRes);
        welfareMicroVo.setPageId(pageId);
        if(pageInfoWithSwitchRes!=null){
            welfareMicroVo.setUpdaterName(EmptyUtils.isEmpty(pageInfoWithSwitchRes.getUpdaterName()) ? pageInfoWithSwitchRes.getCreaterName() : pageInfoWithSwitchRes.getUpdaterName());
            welfareMicroVo.setUpdateTime(EmptyUtils.isEmpty(pageInfoWithSwitchRes.getUpdateTime()) ? pageInfoWithSwitchRes.getCreateTime() : pageInfoWithSwitchRes.getUpdateTime());
            if (EmptyUtils.isNotEmpty(pageInfoWithSwitchRes.getName())) {
                welfareMicroVo.setPageName(pageInfoWithSwitchRes.getName());
            }
            if (EmptyUtils.isNotEmpty(pageInfoWithSwitchRes.getIsHome())) {
                welfareMicroVo.setIsHome(pageInfoWithSwitchRes.getIsHome());
            } else {
                welfareMicroVo.setIsHome(0);
            }
            if (EmptyUtils.isNotEmpty(pageInfoWithSwitchRes.getIsMovie())) {
                welfareMicroVo.setIsMovie(pageInfoWithSwitchRes.getIsMovie());
            } else {
                welfareMicroVo.setIsMovie(0);
            }
            //底部导航栏开关
            if (pageInfoWithSwitchRes.getSwitchBottomLabel().equals(0)) {
                // （如果开关未打开，不返回该值）该写法确保无数据的时候，能够走没有开关的逻辑
            } else {
                if (EmptyUtils.isNotEmpty(pageInfoWithSwitchRes.getLabelName())) {
                    welfareMicroVo.setLabelName(pageInfoWithSwitchRes.getLabelName());
                }
            }

            //金刚区开关
            if (pageInfoWithSwitchRes.getSwitchKingArea().equals(0)) {
                // （如果开关未打开，不返回该值）该写法确保无数据的时候，能够走没有开关的逻辑
            } else {
                // 过滤金刚区类型列表
                List<WelfareMicroManageGoodsVo> kingAreaList = microManageGoodsVoList.stream().filter(welfareMicroManage -> welfareMicroManage.getType() == 2).collect(Collectors.toList());
                if (EmptyUtils.isNotEmpty(kingAreaList)) {
                    // 根据sort升序排序
                    kingAreaList = kingAreaList.stream().sorted(Comparator.comparing(WelfareMicroManage::getSort)).collect(Collectors.toList());
                    welfareMicroVo.setKingAreaList(kingAreaList);
                }
            }

            //广告位开关
            if (pageInfoWithSwitchRes.getSwitchAdvertise().equals(0)) {
                // （如果开关未打开，不返回该值）该写法确保无数据的时候，能够走没有开关的逻辑
            } else {
                // 过滤广告类型列表
                List<WelfareMicroManageGoodsVo> advertiseList = microManageGoodsVoList.stream().filter(welfareMicroManage -> welfareMicroManage.getType() == 3).collect(Collectors.toList());
                if (EmptyUtils.isNotEmpty(advertiseList)) {
                    // 根据sort升序排序
                    advertiseList = advertiseList.stream().sorted(Comparator.comparing(WelfareMicroManage::getSort)).collect(Collectors.toList());
                    welfareMicroVo.setAdvertiseList(advertiseList);
                }
                if (EmptyUtils.isNotEmpty(pageInfoWithSwitchRes.getShowType())) {
                    welfareMicroVo.setShowType(pageInfoWithSwitchRes.getShowType());
                } else {
                    welfareMicroVo.setShowType(1);
                }
            }

            //Tab栏开关
            if (pageInfoWithSwitchRes.getSwitchTab().equals(0)) {
                // （如果开关未打开，不返回该值）该写法确保无数据的时候，能够走没有开关的逻辑
            } else {
                // 过滤Tab类型列表
                List<WelfareMicroManageGoodsVo> tabList = microManageGoodsVoList.stream().filter(welfareMicroManage -> welfareMicroManage.getType() == 4).collect(Collectors.toList());
                if (EmptyUtils.isNotEmpty(tabList)) {
                    // 根据sort升序排序
                    tabList = tabList.stream().sorted(Comparator.comparing(WelfareMicroManage::getSort)).collect(Collectors.toList());
                    welfareMicroVo.setTabList(tabList);
                }
            }

            //公告通知消息开关
            if (pageInfoWithSwitchRes.getSwitchNoticeMsg().equals(0)) {
                // （如果开关未打开，不返回该值）该写法确保无数据的时候，能够走没有开关的逻辑
            } else {
                BaseData baseData = baseDataService.findByCode("homePageSwitchNoticeMsg");
                if(EmptyUtils.isNotEmpty(baseData)){
                    pageInfoWithSwitchRes.setSwitchNoticeMsgTitle(baseData.getName());
                    pageInfoWithSwitchRes.setSwitchNoticeMsgContent(baseData.getValue());
                }
            }
        }

        // 获取微页面对应分区列表
        List<WelfarePartition> welfarePartitionList = mapper.getMicroPagePartitionList(pageId);
        if (EmptyUtils.isNotEmpty(welfarePartitionList)) {
            welfareMicroVo.setPartitionList(welfarePartitionList);
        }
        return welfareMicroVo;
    }

    /*@Override
    public WelfareMicroVo getMicroManagerByPageId(Integer pageId) {
        WelfareMicroVo welfareMicroVo = new WelfareMicroVo();
        List<WelfareMicroManageGoodsVo> microManageGoodsVoList;
        if (pageId.equals(0)) {
            microManageGoodsVoList = mapper.getWelfareMicroMainPageList(pageId);
        } else {
            microManageGoodsVoList = mapper.getWelfareMicroPageList(pageId);
        }
        final Integer finalPageId = pageId;
        if (EmptyUtils.isNotEmpty(microManageGoodsVoList) && microManageGoodsVoList.size() > 0) {
            microManageGoodsVoList.forEach(microManageGoods -> {
                // 如果跳转类型为跳商品，则获取商品Id列表
                if (EmptyUtils.isNotEmpty(microManageGoods.getLinkType())){
                    if ( microManageGoods.getLinkType().equals(4)) {
                        WelfareGoodsPageQuery welfareGoodsPageQuery = new WelfareGoodsPageQuery();
                        welfareGoodsPageQuery.setMicroId(microManageGoods.getId());
                        welfareGoodsPageQuery.setPageSize(99999);
                        WelfareMicroPage welfareMicroPageRes = mapper.selectByPrimaryKey(finalPageId);
                        if (EmptyUtils.isNotEmpty(welfareMicroPageRes) && EmptyUtils.isNotEmpty(welfareMicroPageRes.getIsMovie()) && welfareMicroPageRes.getIsMovie().equals(1)){
                            WelfareMovieInfo movieInfo = welfareGoodsService.getGoodsLinkByMicroIdForMovie(microManageGoods.getId());
                            if (EmptyUtils.isNotEmpty(movieInfo)){
                                microManageGoods.setMovieInfo(movieInfo);
                                if (EmptyUtils.isNotEmpty(movieInfo.getName())){
                                    microManageGoods.setUrlTitle(movieInfo.getName());
                                }
                            }
                        } else {
                            List<WelfareGoodsBaseVo> goodsBaseVoList = welfareGoodsService.getGoodsListPageByMicroId(welfareGoodsPageQuery).getList();
                            if(microManageGoods.getType() == 4){ //如果是底部标签栏，则返回商品数据列表
                                microManageGoods.setGoodsBaseVoList(goodsBaseVoList);
                            }
                            //microManageGoods.setGoodsBaseVoList(goodsBaseVoList);
                            List<Integer> list = new ArrayList<>();
                            List<String> goodsSortlist = new ArrayList<>();
                            if (EmptyUtils.isNotEmpty(goodsBaseVoList)) {
                                goodsBaseVoList.forEach(x -> {
                                    list.add(x.getId());
                                    if(EmptyUtils.isNotEmpty(x.getSort())){
                                        goodsSortlist.add(x.getId()+"_"+x.getSort());
                                    }
                                });
                            }
                            microManageGoods.setGoodsIds(list);
                            microManageGoods.setGoodsSorts(goodsSortlist);
                            //int count = welfareMicroGoodsService.selectGoodsCountByMicroId(microManageGoods.getId());
                            microManageGoods.setUrlTitle(EmptyUtils.isEmpty(list) ? "请选择商品" : "已添加商品" + list.size() + "个");
                        }
                    }else if (microManageGoods.getLinkType() == 1) {
                        WelfarePartition welfarePartition = welfarePartitionService.selectByPrimaryKey(microManageGoods.getPartitionId());
                        if (EmptyUtils.isNotEmpty(welfarePartition)) {
                            microManageGoods.setUrlTitle("分区：" + welfarePartition.getPartitionName());
                        }
                    } else if (microManageGoods.getLinkType() == 2) {
                        String msg = "分类：";
                        if (EmptyUtils.isNotEmpty(microManageGoods.getClassifyId())) {
                            String[] split = microManageGoods.getClassifyId().split(",");
                            for (String s : split) {
                                WelfareGoodsClassify welfareGoodsClassify = welfareGoodsClassifyService.selectByPrimaryKey(s);
                                if (EmptyUtils.isNotEmpty(welfareGoodsClassify)) {
                                    msg = msg + welfareGoodsClassify.getName() + "/";
                                }
                            }
                        }
                        microManageGoods.setUrlTitle(msg);
                    }else if (microManageGoods.getLinkType() == 3){
                        String msg = "分类：";
                        if (EmptyUtils.isNotEmpty(microManageGoods.getClassifyId())) {
                            String[] split = microManageGoods.getClassifyId().split(",");
                            for (String s : split) {
                                WelfareGoodsClassify welfareGoodsClassify = welfareGoodsClassifyService.selectByPrimaryKey(s);
                                if (EmptyUtils.isNotEmpty(welfareGoodsClassify)) {
                                    msg = msg + welfareGoodsClassify.getName() + "/";
                                }
                            }
                        }
                        if (EmptyUtils.isNotEmpty(microManageGoods.getPartitionId())){
                            WelfarePartition welfarePartition = welfarePartitionService.selectByPrimaryKey(microManageGoods.getPartitionId());
                            if (EmptyUtils.isNotEmpty(welfarePartition)) {
                                msg = msg + " 分区：" +welfarePartition.getPartitionName();
                            }
                        }
                        microManageGoods.setUrlTitle(msg);
                    }  else if (microManageGoods.getLinkType() == 5) {
                        microManageGoods.setUrlTitle("URL");
                    } else if (microManageGoods.getLinkType() == 6) {
                        WelfareMicroPage welfareMicroPage = mapper.selectByPrimaryKey(microManageGoods.getLinkPageId());
                        if (EmptyUtils.isNotEmpty(welfareMicroPage)) {
                            microManageGoods.setUrlTitle("微页面：" + welfareMicroPage.getName());
                        }
                    } else if (microManageGoods.getLinkType() == 7) {
                        microManageGoods.setUrlTitle("不跳转");
                    } else if (microManageGoods.getLinkType() == 8) {
                        WelfareMicroPage welfareMicroPage = mapper.selectByPrimaryKey(microManageGoods.getLinkPageId());
                        microManageGoods.setUrlTitle("微页面：" + welfareMicroPage.getName());
                    }
                }else {
                    microManageGoods.setUrlTitle("-");
                }
                if (EmptyUtils.isNotEmpty(microManageGoods.getImg())) {
                    microManageGoods.setImg(ImageUtils.setImageSrc(microManageGoods.getImg()));
                }
            });
            // 过滤轮播类型列表（轮播图不受开关控制）
            List<WelfareMicroManageGoodsVo> shufflingList = microManageGoodsVoList.stream().filter(welfareMicroManage -> welfareMicroManage.getType() == 1).collect(Collectors.toList());
            if (EmptyUtils.isNotEmpty(shufflingList)) {
                // 根据sort升序排序
                shufflingList = shufflingList.stream().sorted(Comparator.comparing(WelfareMicroManage::getSort)).collect(Collectors.toList());
                welfareMicroVo.setShufflingList(shufflingList);
            }
        }
        if (pageId.equals(0)) {
            pageId = microManageGoodsVoList.get(0).getPageId();
        }
        // 获取开关和微页面信息
        WelfareMicroPageWithSwitchVo pageInfoWithSwitchRes = mapper.getMicroPageInfoWithSwitch(pageId);
        welfareMicroVo.setSwitchTab(pageInfoWithSwitchRes);
        if (EmptyUtils.isNotEmpty(pageInfoWithSwitchRes) && pageInfoWithSwitchRes.getSwitchBottomLabel() != null && pageInfoWithSwitchRes.getSwitchBottomLabel().equals(0)) {
            // （如果开关未打开，不返回该值）该写法确保无数据的时候，能够走没有开关的逻辑
        } else {
            if (EmptyUtils.isNotEmpty(pageInfoWithSwitchRes.getLabelName())) {
                welfareMicroVo.setLabelName(pageInfoWithSwitchRes.getLabelName());
            }
            if (EmptyUtils.isNotEmpty(pageInfoWithSwitchRes.getSwitchBottomLabel())) {
                welfareMicroVo.getSwitchTab().setSwitchBottomLabel(pageInfoWithSwitchRes.getSwitchBottomLabel());
            } else {
                welfareMicroVo.getSwitchTab().setSwitchBottomLabel(0);
            }
        }
        if (EmptyUtils.isNotEmpty(pageInfoWithSwitchRes.getName())) {
            welfareMicroVo.setPageName(pageInfoWithSwitchRes.getName());
        }
        welfareMicroVo.setPageId(pageId);
        if (EmptyUtils.isNotEmpty(pageInfoWithSwitchRes.getIsHome())) {
            welfareMicroVo.setIsHome(pageInfoWithSwitchRes.getIsHome());
        } else {
            welfareMicroVo.setIsHome(0);
        }
        if (EmptyUtils.isNotEmpty(pageInfoWithSwitchRes.getIsMovie())){
            welfareMicroVo.setIsMovie(pageInfoWithSwitchRes.getIsMovie());
        } else {
            welfareMicroVo.setIsMovie(0);
        }
        if (EmptyUtils.isNotEmpty(pageInfoWithSwitchRes) && pageInfoWithSwitchRes.getSwitchKingArea() != null && pageInfoWithSwitchRes.getSwitchKingArea().equals(0)) {
            // （如果开关未打开，不返回该值）该写法确保无数据的时候，能够走没有开关的逻辑
        } else {
            // 过滤金刚区类型列表
            List<WelfareMicroManageGoodsVo> kingAreaList = microManageGoodsVoList.stream().filter(welfareMicroManage -> welfareMicroManage.getType() == 2).collect(Collectors.toList());
            if (EmptyUtils.isNotEmpty(kingAreaList)) {
                // 根据sort升序排序
                kingAreaList = kingAreaList.stream().sorted(Comparator.comparing(WelfareMicroManage::getSort)).collect(Collectors.toList());
                welfareMicroVo.setKingAreaList(kingAreaList);
            }
            if (EmptyUtils.isNotEmpty(pageInfoWithSwitchRes.getSwitchKingArea())) {
                welfareMicroVo.getSwitchTab().setSwitchKingArea(pageInfoWithSwitchRes.getSwitchKingArea());
            } else {
                welfareMicroVo.getSwitchTab().setSwitchKingArea(0);
            }
        }
        if (EmptyUtils.isNotEmpty(pageInfoWithSwitchRes) && pageInfoWithSwitchRes.getSwitchAdvertise() != null && pageInfoWithSwitchRes.getSwitchAdvertise().equals(0)) {
            // （如果开关未打开，不返回该值）该写法确保无数据的时候，能够走没有开关的逻辑
        } else {
            // 过滤广告类型列表
            List<WelfareMicroManageGoodsVo> advertiseList = microManageGoodsVoList.stream().filter(welfareMicroManage -> welfareMicroManage.getType() == 3).collect(Collectors.toList());
            if (EmptyUtils.isNotEmpty(advertiseList)) {
                // 根据sort升序排序
                advertiseList = advertiseList.stream().sorted(Comparator.comparing(WelfareMicroManage::getSort)).collect(Collectors.toList());
                welfareMicroVo.setAdvertiseList(advertiseList);
            }
            if (EmptyUtils.isNotEmpty(pageInfoWithSwitchRes.getSwitchAdvertise())) {
                welfareMicroVo.getSwitchTab().setSwitchAdvertise(pageInfoWithSwitchRes.getSwitchAdvertise());
            } else {
                welfareMicroVo.getSwitchTab().setSwitchAdvertise(0);
            }
            if (EmptyUtils.isNotEmpty(pageInfoWithSwitchRes.getShowType())) {
                welfareMicroVo.setShowType(pageInfoWithSwitchRes.getShowType());
            } else {
                welfareMicroVo.setShowType(1);
            }
        }
        if (EmptyUtils.isNotEmpty(pageInfoWithSwitchRes) && pageInfoWithSwitchRes.getSwitchTab() != null && pageInfoWithSwitchRes.getSwitchTab().equals(0)) {
            // （如果开关未打开，不返回该值）该写法确保无数据的时候，能够走没有开关的逻辑
        } else {
            // 过滤Tab类型列表
            List<WelfareMicroManageGoodsVo> tabList = microManageGoodsVoList.stream().filter(welfareMicroManage -> welfareMicroManage.getType() == 4).collect(Collectors.toList());
            if (EmptyUtils.isNotEmpty(tabList)) {
                // 根据sort升序排序
                tabList = tabList.stream().sorted(Comparator.comparing(WelfareMicroManage::getSort)).collect(Collectors.toList());
                welfareMicroVo.setTabList(tabList);
            }
            if (EmptyUtils.isNotEmpty(pageInfoWithSwitchRes.getSwitchTab())) {
                welfareMicroVo.getSwitchTab().setSwitchTab(pageInfoWithSwitchRes.getSwitchTab());
            } else {
                welfareMicroVo.getSwitchTab().setSwitchTab(0);
            }
        }
        welfareMicroVo.setUpdaterName(EmptyUtils.isEmpty(pageInfoWithSwitchRes.getUpdaterName()) ? pageInfoWithSwitchRes.getCreaterName() : pageInfoWithSwitchRes.getUpdaterName());
        welfareMicroVo.setUpdateTime(EmptyUtils.isEmpty(pageInfoWithSwitchRes.getUpdateTime()) ? pageInfoWithSwitchRes.getCreateTime() : pageInfoWithSwitchRes.getUpdateTime());
        // 获取微页面对应分区列表
        List<WelfarePartition> welfarePartitionList = mapper.getMicroPagePartitionList(pageId);
        if (EmptyUtils.isNotEmpty(welfarePartitionList)) {
            welfareMicroVo.setPartitionList(welfarePartitionList);
        }
        return welfareMicroVo;
    }*/

    @Override
    public List<WelfareGoodsClassifyVo> getGoodsClassifyForMicro(Integer pageId,Integer enterpriseId) {
        // 获取微页面对应分区列表
        List<WelfarePartition> welfarePartitionList = mapper.getMicroPagePartitionList(pageId);
        List<Integer> partitionIdList = new ArrayList<>();
        if (EmptyUtils.isNotEmpty(welfarePartitionList) && welfarePartitionList.size() > 0) {
            // 获取分区列表
            welfarePartitionList.forEach(welfarePartition -> {
                partitionIdList.add(welfarePartition.getId());
            });
            // 如果配置的是全部分区，则走原来的获取分类列表
            if (partitionIdList.contains(0)) {
                return welfareGoodsClassifyService.getGoodsClassifyWithOutEmptySub(enterpriseId);
            } else {
                List<Integer> goodsClassifyThirdIdList = welfareGoodsService.getGoodsThirdClassifyIdsByPartitions(partitionIdList);
                if (EmptyUtils.isNotEmpty(goodsClassifyThirdIdList) && goodsClassifyThirdIdList.size() > 0) {
                    return welfareGoodsClassifyService.getGoodsClassifyByThirdIdList(goodsClassifyThirdIdList);
                } else {
                    return new ArrayList<>();
                }
            }
        } else {
            return new ArrayList<>();
        }
    }

    @Override
    public List<WelfareBrand> getBrandsByClassifyForMicro(WelfareGoodsPageQuery welfareGoodsQuery) {
        // 跳转类型进来获取品牌，不需要考虑分区
        if (EmptyUtils.isNotEmpty(welfareGoodsQuery.getIsLink()) && welfareGoodsQuery.getIsLink() == 1) {
            return welfareGoodsService.getBrandListByGoodsQuery(welfareGoodsQuery);
        }
        // 分类入口获取品牌，需要考虑分区
        else {
            // 判断是不是主页（如果是主页不考虑分区配置）
            if (EmptyUtils.isNotEmpty(welfareGoodsQuery.getPageId()) && welfareGoodsQuery.getPageId() != 0) {
                List<WelfarePartition> welfarePartitionList = mapper.getMicroPagePartitionList(welfareGoodsQuery.getPageId());
                if ((EmptyUtils.isEmpty(welfarePartitionList) || welfarePartitionList.size() < 1)) {
                    throw new CommonException("无效分区");
                }
                List<Integer> partitionIdList = new ArrayList<>();
                // 获取分区列表
                welfarePartitionList.forEach(welfarePartition -> {
                    partitionIdList.add(welfarePartition.getId());
                });
                // 如果配置的是全部分区
                if (partitionIdList.contains(0)) {
                    return welfareGoodsService.getBrandListByGoodsQuery(welfareGoodsQuery);
                } else {
                    welfareGoodsQuery.setPartitionIds(partitionIdList);
                    return welfareGoodsService.getBrandListByGoodsQuery(welfareGoodsQuery);
                }
            } else {
                return welfareGoodsService.getBrandListByGoodsQuery(welfareGoodsQuery);
            }
        }
    }

    @Override
    public PageInfo<WelfareMicroPage> getPageListData(WelfareMicroPageQuery welfareMicroPageQuery) {
        PageHelper.startPage(welfareMicroPageQuery);
        List<WelfareMicroPage> list = mapper.getPageListData(welfareMicroPageQuery);
        PageInfo<WelfareMicroPage> page = new PageInfo<WelfareMicroPage>(list);
        return page;
    }

    private Integer getGoodsSort(List<String> goodsSorts, Integer goodsId){
        Integer sort = null;
        if(EmptyUtils.isNotEmpty(goodsSorts)){
            for (String x:goodsSorts) {
                if(x.startsWith(goodsId+"_")){
                    sort = Integer.valueOf(x.split("_")[1]);
                    break;
                }
            }
        }
        return sort;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer saveMicroPage(WelfareMicroVo welfareMicroVo, String updaterName) {
        List<WelfareMicroManageGoodsVo> shufflingList = welfareMicroVo.getShufflingList();
        List<WelfareMicroManageGoodsVo> kingAreaList = welfareMicroVo.getKingAreaList();
        List<WelfareMicroManageGoodsVo> advertiseList = welfareMicroVo.getAdvertiseList();
        List<WelfareMicroManageGoodsVo> tabList = welfareMicroVo.getTabList();
        WelfareMicroPageWithSwitchVo switchTab = welfareMicroVo.getSwitchTab();
        List<Integer> partitionIdList = welfareMicroVo.getPartitionIdList();
        //合并list
        List<WelfareMicroManageGoodsVo> welfareMicroManageList =
                Stream.of(shufflingList, kingAreaList, advertiseList, tabList)
                        .flatMap(Collection::stream).collect(Collectors.toList());
        //处理list
        //根据pageId查询该页面是否配置过
        WelfareMicroManage welfareMicroManage = new WelfareMicroManage();
        welfareMicroManage.setPageId(welfareMicroVo.getPageId());
        welfareMicroManage.setStatus(1);
        int count = welfareMicroManageService.selectCount(welfareMicroManage);
        //配置过，进行编辑修改  先删除老数据
        if (count > 0) {
            welfareMicroManageService.delete(welfareMicroManage);
            // TODO 判断是否需要删除多商品表
        }
        if (EmptyUtils.isNotEmpty(welfareMicroManageList)) {
            //新的goodsIds
            List<WelfareMicroGoods> goodsList = new ArrayList<>();
            for (WelfareMicroManageGoodsVo manage : welfareMicroManageList) {
                manage.setId(null);
                manage.setUpdaterName(updaterName);
                manage.setCreateTime(new Date());
                manage.setStatus(1);
                manage.setPageId(welfareMicroVo.getPageId());
                //保存manage表
                welfareMicroManageService.insertSelective(manage);
                if (EmptyUtils.isNotEmpty(manage.getGoodsIds())) {
                    manage.getGoodsIds().forEach(goodsId -> {
                        if (EmptyUtils.isNotEmpty(goodsId)) {
                            WelfareMicroGoods welfareMicroGoods = new WelfareMicroGoods();
                            welfareMicroGoods.setMicroId(manage.getId());
                            welfareMicroGoods.setGoodsId(goodsId);
                            welfareMicroGoods.setStatus(1);
                            welfareMicroGoods.setCreateTime(new Date());
                            welfareMicroGoods.setSort(getGoodsSort(manage.getGoodsSorts(),goodsId));
                            goodsList.add(welfareMicroGoods);
                        }
                    });
                }
                //删除原有商品跳转数据
                WelfareMicroGoods welfareMicroGoods = new WelfareMicroGoods();
                welfareMicroGoods.setMicroId(manage.getId());
                welfareMicroGoods.setStatus(1);
                welfareMicroGoodsService.delete(welfareMicroGoods);
            }
            //保存商品跳转
            if (EmptyUtils.isNotEmpty(goodsList)) {
                welfareMicroGoodsService.insertList(goodsList);
            }
            //处理switchTab
            WelfareMicroSwitch welfareMicroSwitch = new WelfareMicroSwitch();
            welfareMicroSwitch.setPageId(welfareMicroVo.getPageId());
            welfareMicroSwitch.setStatus(1);
            List<WelfareMicroSwitch> select = welfareMicroSwitchService.select(welfareMicroSwitch);
            //存在就编辑修改
            if (EmptyUtils.isNotEmpty(select)) {
                select.get(0).setSwitchKingArea(switchTab.getSwitchKingArea());
                select.get(0).setSwitchAdvertise(switchTab.getSwitchAdvertise());
                select.get(0).setSwitchTab(switchTab.getSwitchTab());
                select.get(0).setSwitchBottomLabel(switchTab.getSwitchBottomLabel());
                select.get(0).setSwitchNoticeMsg(switchTab.getSwitchNoticeMsg());
                welfareMicroSwitchService.updateByPrimaryKeySelective(select.get(0));
            } else {
                welfareMicroSwitch.setSwitchKingArea(switchTab.getSwitchKingArea());
                welfareMicroSwitch.setSwitchAdvertise(switchTab.getSwitchAdvertise());
                welfareMicroSwitch.setSwitchTab(switchTab.getSwitchTab());
                welfareMicroSwitch.setSwitchBottomLabel(switchTab.getSwitchBottomLabel());
                welfareMicroSwitch.setSwitchNoticeMsg(switchTab.getSwitchNoticeMsg());
                welfareMicroSwitch.setCreateTime(new Date());
                welfareMicroSwitchService.insertSelective(welfareMicroSwitch);
            }
            if(EmptyUtils.isNotEmpty(switchTab.getSwitchNoticeMsg()) && switchTab.getSwitchNoticeMsg()==CommonConstants.YES){
                //保存公告通知内容
                if(EmptyUtils.isNotEmpty(switchTab.getSwitchNoticeMsgTitle()) && EmptyUtils.isNotEmpty(switchTab.getSwitchNoticeMsgContent())){
                    BaseData baseData = baseDataService.findByCode("homePageSwitchNoticeMsg");
                    if(EmptyUtils.isNotEmpty(baseData)){
                        baseData.setName(switchTab.getSwitchNoticeMsgTitle());
                        baseData.setValue(switchTab.getSwitchNoticeMsgContent());
                        baseData.setUpdateTime(new Date());
                        baseDataService.updateByPrimaryKeySelective(baseData);
                    }else{
                        baseData = new BaseData();
                        baseData.setCode("homePageSwitchNoticeMsg");
                        baseData.setName(switchTab.getSwitchNoticeMsgTitle());
                        baseData.setRemark("微页面首页公告通知内容");
                        baseData.setStatus(CommonConstants.YES);
                        baseData.setCreateTime(new Date());
                        baseData.setValue(switchTab.getSwitchNoticeMsgContent());
                        baseData.setUpdateTime(new Date());
                        baseDataService.insertSelective(baseData);
                    }
                }else{
                    throw new CommonException("公告通知标题或内容不能为空");
                }
            }
            //处理page表
            WelfareMicroPage welfareMicroPage = mapper.selectByPrimaryKey(welfareMicroVo.getPageId());
            welfareMicroPage.setShowType(welfareMicroVo.getShowType());
            welfareMicroPage.setName(welfareMicroVo.getPageName());
            welfareMicroPage.setLabelName(welfareMicroVo.getLabelName());
            welfareMicroPage.setUpdateTime(new Date());
            welfareMicroPage.setUpdaterName(updaterName);
            mapper.updateByPrimaryKeySelective(welfareMicroPage);
            //处理分区表
            WelfareMicroPartition welfareMicroPartition = new WelfareMicroPartition();
            welfareMicroPartition.setStatus(1);
            welfareMicroPartition.setPageId(welfareMicroVo.getPageId());
            List<WelfareMicroPartition> select1 = welfareMicroPartitionService.select(welfareMicroPartition);
            if (EmptyUtils.isNotEmpty(select1)) {
                welfareMicroPartitionService.delete(welfareMicroPartition);
            }
            if (EmptyUtils.isNotEmpty(partitionIdList)) {
                List<WelfareMicroPartition> list = new ArrayList<>();
                partitionIdList.forEach(x -> {
                    WelfareMicroPartition partition = new WelfareMicroPartition();
                    partition.setPageId(welfareMicroVo.getPageId());
                    partition.setPartitionId(x);
                    partition.setStatus(1);
                    partition.setCreateTime(new Date());
                    list.add(partition);
                });
                welfareMicroPartitionService.insertList(list);
            }
        }
        return 1;
    }
}
